package com.music.controller;

import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Controller;
import com.laoxuai.ibatis.util.PageInfo;
import com.laoxuai.web.mvc.annotation.RequestMapping;
import com.laoxuai.web.mvc.annotation.ResponseBody;
import com.music.pojo.Artist;
import com.music.pojo.Tag;
import com.music.service.ArtistService;
import com.music.util.Result;

import java.util.List;

@Controller
@RequestMapping("/artist")
public class ArtistController {

    @Autowired
    private ArtistService artistServiceImpl;

    /***
     * 新增
     * @param artist
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(Artist artist) {
        int result = artistServiceImpl.insert(artist);
        if(result==-1){
            return Result.failed("歌手姓名不能为空");
        }else{
            return Result.success("添加成功", result);
        }
    }

    /***
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(Integer id) {
        int result = artistServiceImpl.delete(id);
        if(result==-1){
            return Result.failed("删除失败");
        }else {
            return Result.success("删除成功", result);
        }
    }

    /***
     * 修改
     * @param artist
     * @param id
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Result update(Integer id, Artist artist) {
        artist.setId(id);
        int result = artistServiceImpl.update(artist);
        if(result==-1){
            return Result.failed("歌手已存在");
        }else{
            return Result.success("修改成功", result);
        }
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/selectAll")
    @ResponseBody
    public Result selectAll(){
        List<Artist> list = artistServiceImpl.selectAll();
        if (list == null){
            return Result.failed("查询失败");
        }
        return Result.success("查询成功",list);
    }

    /***
     * 主键查询
     * @param id
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public Result getByPrimaryKey(Integer id) {
        Artist artist = artistServiceImpl.getByPrimaryKey(id);
        return Result.success("查询成功", artist);
    }

    @RequestMapping(value = "/page")
    @ResponseBody
    public Result page(Artist artist, int page, int size) {
        PageInfo<Artist> pageInfo = artistServiceImpl.listPageInfo(artist, page, size);
        return Result.success("查询成功", pageInfo);
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Result list(Artist artist, int page, int size) {
        List<Artist> list = artistServiceImpl.listPage(artist, page, size);
        return Result.success("查询成功", list);
    }
}